package com.fitso.model.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Before;
import org.junit.Test;

import com.fitso.model.bean.measure.energy.Calorie;
import com.fitso.model.bean.measure.mass.Gram;
import com.fitso.model.bean.measure.mass.MilliGram;
import com.fitso.model.bean.measure.volume.FluidOunce;
import com.fitso.model.bean.nutrition.Carbohydrates;
import com.fitso.model.bean.nutrition.Cholesterol;
import com.fitso.model.bean.nutrition.Fats;
import com.fitso.model.bean.nutrition.Food;
import com.fitso.model.bean.nutrition.FoodDescription;
import com.fitso.model.bean.nutrition.Minerals;
import com.fitso.model.bean.nutrition.Protein;
import com.fitso.model.bean.nutrition.Serving;

public abstract class FoodDescriptionDaoDB_IT extends DatabaseIntegrationTestUnit {
	protected FoodIdentityDao foodIdentityDao;
	protected FoodDao foodDao;

	public abstract FoodDao newFoodDao();

	public abstract FoodIdentityDao newFoodIdentityDao();

	@Before
	public void setUp() throws Exception {
		super.setUp();

		foodIdentityDao = newFoodIdentityDao();
		foodDao = newFoodDao();
	}

	@Test
	public void test_saveOrUpdate() throws Exception {
		Food vanillaLatte = new Food();
		vanillaLatte.setDescription(new FoodDescription("Latte", "Starbucks", "Vanilla Latte"));
		vanillaLatte.setServing(new Serving(new FluidOunce(16), "Grande"));
		vanillaLatte.setCalories(new Calorie(144));

		Carbohydrates carbs = new Carbohydrates(new Gram(18));
		carbs.setSugar(new Gram(16.8));
		vanillaLatte.setCarbs(carbs);

		vanillaLatte.setCholesterol(new Cholesterol(new MilliGram(18)));

		Fats fats = new Fats(new Gram(5.6));
		fats.setSaturated(new Gram(3.2));
		vanillaLatte.setFats(fats);

		Minerals mins = new Minerals();
		mins.setSodium(new MilliGram(68));
		mins.setCalcium(new MilliGram(200.2));
		vanillaLatte.setMinerals(mins);

		vanillaLatte.setProtein(new Protein(new Gram(6)));
		foodDao.createOrUpdate(vanillaLatte);

		FoodDescription id = new FoodDescription();
		id.setCommonName("Latte");
		id.setProducer("Starbucks");
		id.setProductName("Vanilla Late");

		foodIdentityDao.createOrUpdate(id);

		FoodDescription fetchedId = foodIdentityDao.readById(id.getId());
		assertNotNull(fetchedId);
		assertEquals(id, fetchedId);
	}
}
